/*1*/ package Vista;
/*2*/
/*3*/ import Controlador.ControladorDefinirCasosDeUso;
/*4*/ import DTO.CasoDeUsoDTO;
/*5*/ import java.awt.Color;
import java.awt.Component;
/*6*/ import java.awt.event.WindowAdapter;
/*7*/ import java.awt.event.WindowEvent;
/*8*/ import java.io.File;
/*9*/ import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
/*10*/ import javax.swing.JOptionPane;
/*11*/ import javax.swing.JTable;
/*12*/ import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableCellRenderer;
/*13*/

public class JPDefinirCasosDeUso extends javax.swing.JPanel {

    /*20*/     ControladorDefinirCasosDeUso control;
/*21*/     int posCU = 0;

    /** Creates new form JPDefinirCasosDeUso */
    public JPDefinirCasosDeUso(ControladorDefinirCasosDeUso controlador) {
/*28*/         this.control = controlador;
/*29*/         //seteo de datos de la tabla
/*30*/         initComponents();
/*31*/         setFilas();
/*32*/         this.bPrev.setEnabled(false);
/*33*/         if (this.control.getCasosDeUso() == null) {
/*34*/             setCU();
/*35*/         }
/*36*/         this.setTamTabla();
  /*37*/         this.setCU(0);
/*38*/         //si se cierra la ventana se guarda la sesion en un archivo
///////////////////////*39*/         this.addWindowListener(new WindowAdapter() {
///////////////////////*40*/
///////////////////////*41*/             @Override
///////////////////////*42*/             public void windowClosing(WindowEvent e) {
///////////////////////*43*/                 javax.swing.JFileChooser jF1 = new javax.swing.JFileChooser();
///////////////////////*44*/                 String ruta = "";
///////////////////////*45*/                 try {
///////////////////////*46*/                     if (jF1.showSaveDialog(null) == jF1.APPROVE_OPTION) {
///////////////////////*47*/                         ruta = jF1.getSelectedFile().getAbsolutePath();
///////////////////////*48*/                         if (new File(ruta).exists()) {
///////////////////////*49*/                             if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(null, "El fichero existe,deseas reemplazarlo?", "Titulo", JOptionPane.YES_NO_OPTION)) {
///////////////////////*50*/                                 control.archivoSesion(ruta);
///////////////////////*51*/                             }
///////////////////////*52*/                         } else {
///////////////////////*53*/                             control.archivoSesion(ruta);
///////////////////////*54*/                         }
///////////////////////*55*/                     }
///////////////////////*56*/                 } catch (Exception ex) {
///////////////////////*57*/                     ex.printStackTrace();
///////////////////////*58*/                 }
///////////////////////*59*/
///////////////////////*60*/                 System.exit(0);
///////////////////////*61*/             }
///////////////////////*62*/         });
/*63*/
/*64*/     }
    
          
           
          
/*65*/
/*66*/         /**
                 * Muestra cada uno de los componentes del caso de uso en cada una de las filas
                 */
/*69*/     public void setFilas() {
/*70*/         DefaultTableModel tabla = new MyTableModel(new Object[][]{
/*71*/                     {"1","Caso de Uso", null},
/*72*/                     {"2","Versión", null},
/*73*/                     {"3","Resumen", null},
/*74*/                     {"4","Actores", null},
/*75*/                     {"5","Precondiciones", null},
/*76*/                     {"6","Descripción", null},
/*77*/                     {"7","Excepciones", null},
/*78*/                     {"8","Post-condiciones", null}
/*79*/                 },
/*80*/                 new String[]{
/*81*/                     "Número","Componente", "Descripción"});
/*82*/         tabla.setRowCount(8);
/*83*/         tabla.setColumnCount(3);
/*84*/         this.tCasoDeUso.setModel(tabla);
              TableColumn col= tCasoDeUso.getColumn("Número");
                col.setMaxWidth(50);
                
                tCasoDeUso.getColumn("Descripción").setCellRenderer(new YourTableCellRenderer());  
/*85*/     }
/*86*/
/*87*/         /**
                 * Clase para generar las tabla
                 */
/*90*/     class MyTableModel extends DefaultTableModel {
/*91*/
/*92*/         public MyTableModel(Object[][] data, Object[] columnNames) {
/*93*/             super(data, columnNames);
/*94*/         }
/*95*/
/*96*/         /**
                 *
                 * @param data
                 * @param columnNames
                 */
/*102*/         @Override
        /**
        * Si la fila es la quinta y la columna la primera, retorna true, caso contrario; false.
        */
/*103*/         public boolean isCellEditable(int row, int col) {
/*104*/             if (row == 5 && col == 2) {
/*105*/                 return true;
/*106*/             } else {
/*107*/                 return false;
/*108*/             }
/*109*/         }
/*110*/     }
/*111*/
/*112*/     //seteo del primer caso de uso
/*113*/         /**
                 * Seteo del primer caso de uso
                 */
/*116*/     public void setCU() {
/*117*/         this.control.setCasosDeUsoInicial();
/*118*/     }
/*119*/
/*120*/     //se obtiene la posicion del caso de uso
/*121*/         /**
             * Se obtiene la posicion del caso de uso
             * @return La posición de dicho caso de uso
             */
/*125*/     public int getPos() {
/*126*/         return posCU;
/*127*/     }
/*128*/
/*129*/
/*130*/     /**
             * se setea el tamaño de la tabla
             */
/*133*/     public void setTamTabla() {
/*134*/         this.tCasoDeUso.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
/*135*/         this.tCasoDeUso.getColumnModel().getColumn(1).setPreferredWidth(200);
/*136*/         this.tCasoDeUso.getColumnModel().getColumn(2).setPreferredWidth(690);
/*137*/        // this.tCasoDeUso.getColumnModel().getColumn(3).setCellRenderer(new TextAreaRenderer());
/*138*/         this.tCasoDeUso.setShowGrid(true);
/*139*/         this.tCasoDeUso.setGridColor(Color.black);
/*140*/     }
/*141*/
/*142*/
/*143*/         /**
                 * Seteo del caso de uso de una sesion anterior
                 * @param pos Indica que número de caso de uso es.
                 */
/*147*/     public void setCU(int pos) {
/*148*/         //Vector v=this.control.getCasosDeUso();
/*149*/         Vector v = this.control.casoUsoMod();
/*150*/         this.lNumCU.setText("Caso de uso Nº " + (pos + 1) + " de " + v.size() + ".");
/*151*/         CasoDeUsoDTO cu = (CasoDeUsoDTO) v.get(pos);
/*152*/         this.tCasoDeUso.setValueAt(cu.getNombre(), 0, 2);
/*153*/         this.tCasoDeUso.setValueAt(cu.getVersion(), 1, 2);
/*154*/         this.tCasoDeUso.setValueAt(cu.getResumen(), 2, 2);
/*155*/         this.tCasoDeUso.setValueAt(cu.getActores(), 3, 2);
/*156*/         this.tCasoDeUso.setValueAt(cu.getPrecondiciones(), 4, 2);
/*157*/         this.tCasoDeUso.setValueAt(cu.getDescripcion(), 5, 2);
/*158*/         this.tCasoDeUso.setValueAt(cu.getExcepciones(), 6, 2);
/*159*/         this.tCasoDeUso.setValueAt(cu.getPostcondiciones(), 7, 2);
/*160*/
/*161*/     }
/*162*/
/*163*/     //verificar celdas seleccionadas
/*164*/         /**
                 * Verificar celdas seleccionadas
                 * @return 1 si está seleccionada, 0 si no.
                 */
/*168*/     public int select() {
/*169*/         for (int i = 0; i < this.tCasoDeUso.getRowCount(); i++) {
/*170*/             if (this.tCasoDeUso.isCellSelected(i, 2)) {
/*171*/                 return 1;
/*172*/             }
/*173*/         }
/*174*/         return 0;
/*175*/     }
/*176*/
/*177*/     //obtener casos de uso
/*178*/         /**
                 * Obtener casos de uso
                 * @return Vector con el caso de uso completo
                 */
/*182*/     public Vector getCU() {
/*183*/         Vector cu = new Vector();
/*184*/         for (int i = 0; i < this.tCasoDeUso.getRowCount(); i++) {
/*185*/             cu.add(this.tCasoDeUso.getValueAt(i, 2));
/*186*/         }
/*187*/         return cu;
/*188*/
/*189*/     }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tCasoDeUso = new javax.swing.JTable();
        bPrev = new javax.swing.JButton();
        bNext = new javax.swing.JButton();
        lNumCU = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(proyectodetitulo.ProyectoDeTituloApp.class).getContext().getResourceMap(JPDefinirCasosDeUso.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tCasoDeUso.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Caso de Uso", null},
                {"Versión", null},
                {"Resumen", null},
                {"Actores", null},
                {"Precondiciones", null},
                {"Descripción", null},
                {"Excepciones", null},
                {"Post-condiciones", null}
            },
            new String [] {
                "Componente", "Descripción"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tCasoDeUso.setCellSelectionEnabled(true);
        tCasoDeUso.setName("tCasoDeUso"); // NOI18N
        jScrollPane1.setViewportView(tCasoDeUso);
        tCasoDeUso.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        bPrev.setText(resourceMap.getString("bPrev.text")); // NOI18N
        bPrev.setName("bPrev"); // NOI18N
        bPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bPrevActionPerformed(evt);
            }
        });

        bNext.setText(resourceMap.getString("bNext.text")); // NOI18N
        bNext.setName("bNext"); // NOI18N
        bNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bNextActionPerformed(evt);
            }
        });

        lNumCU.setFont(resourceMap.getFont("lNumCU.font")); // NOI18N
        lNumCU.setText(resourceMap.getString("lNumCU.text")); // NOI18N
        lNumCU.setName("lNumCU"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setName("jLabel5"); // NOI18N

        jLabel7.setIcon(resourceMap.getIcon("jLabel7.icon")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lNumCU)
                        .addGap(426, 426, 426)
                        .addComponent(jLabel7))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(403, 403, 403)
                                .addComponent(bPrev)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(bNext)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 889, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(262, 262, 262)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(74, 74, 74)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(167, 167, 167)
                                        .addComponent(jLabel4))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(120, 120, 120)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel3))))
                                .addGap(185, 185, 185))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(268, 268, 268)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(50, 50, 50)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lNumCU)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4)
                                .addComponent(jLabel5)))
                        .addGap(21, 21, 21))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bPrev)
                            .addComponent(bNext)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)))
                .addContainerGap(90, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 932, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bPrevActionPerformed
        /*646*/         //ir a caso de uso anterior
        /*647*/         if (this.select() == 1) {
            /*648*/             this.tCasoDeUso.clearSelection();
            /*649*/             if (this.tCasoDeUso.getCellEditor() != null) {
                /*650*/                 this.tCasoDeUso.getCellEditor().stopCellEditing();
                /*651*/             }
            /*652*/         }
        /*653*/         posCU--;
        /*654*/         this.bNext.setEnabled(true);
        /*655*/         if (posCU == 0) {
            /*656*/             this.setCU(posCU);
            /*657*/             this.bPrev.setEnabled(false);
            /*658*/         } else {
            /*659*/             this.setCU(posCU);
            /*660*/         }
}//GEN-LAST:event_bPrevActionPerformed

    private void bNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bNextActionPerformed
        /*668*/         //ir a caso de uso posterior
        /*669*/         if (this.select() == 1) {
            /*670*/             this.tCasoDeUso.clearSelection();
            /*671*/             if (this.tCasoDeUso.getCellEditor() != null) {
                /*672*/                 this.tCasoDeUso.getCellEditor().stopCellEditing();
                /*673*/             }
            /*674*/         }
        /*675*/         posCU++;
        /*676*/         this.bPrev.setEnabled(true);
        /*677*/         Vector v = this.control.getCasosDeUso();
        /*678*/         if (posCU == v.size() - 1) {
            /*679*/             this.setCU(posCU);
            /*680*/             this.bNext.setEnabled(false);
            /*681*/         } else {
            /*682*/             this.setCU(posCU);
            /*683*/         }
}//GEN-LAST:event_bNextActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
 /*750*/         //se genera el xml con los casos de uso generados
        /*751*/         javax.swing.JFileChooser jF1 = new javax.swing.JFileChooser();
                        /* Se agrega filtro de extension XML */
                        FileFilter XMLFilter = new ExtensionFileFilter( "Archivo XML", new String[] {"XML"});
                        jF1.addChoosableFileFilter(XMLFilter);
                        /**************************************/

        /*752*/         String ruta = "";
        /*753*/         try {
            /*754*/             if (jF1.showSaveDialog(null) == jF1.APPROVE_OPTION) {
                /*755*/                 ruta = jF1.getSelectedFile().getAbsolutePath();

                                        /* Agrega extension al archivo a guardar .xml  */
                                        ruta = ruta + ".xml";
                                        /*************************/

                /*756*/                 if (new File(ruta).exists()) {
                    /*757*/                     if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(this, "El fichero existe,desea reemplazarlo?", "Titulo", JOptionPane.YES_NO_OPTION)) {
                        /*758*/                         this.control.generarXML(ruta);
                        /*759*/                     }
                    /*760*/                 } else {
                    /*761*/                     this.control.generarXML(ruta);
                    /*762*/                 }
                /*763*/             }
            /*764*/         } catch (Exception ex) {
                /*765*/             ex.printStackTrace();
                /*766*/         }
    }//GEN-LAST:event_jButton1ActionPerformed

    /* public boolean validarExc()
           {
           if (tCasoDeUso )
                   {
                   return true;   
                   }
           return false;
           }  
      }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bNext;
    private javax.swing.JButton bPrev;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lNumCU;
    private javax.swing.JTable tCasoDeUso;
    // End of variables declaration//GEN-END:variables

}
